
//
// Copyright (c) 2008, the /*jupiter jazz*/ group
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the /*jupiter jazz*/ group nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY the /*jupiter jazz*/ group ''AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL the /*jupiter jazz*/ group BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

#ifndef CLASSLIB_EXCEPTION_H
#define CLASSLIB_EXCEPTION_H

// Standard headers.
#include <cassert>
#include <cstddef>
#include <cstring>
#include <exception>

namespace foundation
{

//
// Base class for all exceptions.
//

class Exception
  : public std::exception
{
  public:
    // Constructor, make a copy of the string passed in argument.
    explicit Exception(const char* cause = "unknown cause")
    {
        assert(cause);
        std::strncpy(m_cause, cause, MaxCauseStringLength);
    }

    // Return a string describing the cause of the exception.
    virtual const char* what() const
      throw ()
    {
        return m_cause;
    }

  private:
    static const size_t MaxCauseStringLength = 256;
    char m_cause[MaxCauseStringLength + 1];
};


//
// An exception carrying an arbitrary string.
//

class StringException
  : public Exception
{
  public:
    // Constructor, make a copy of the string passed in argument.
    StringException(
        const char* cause,
        const char* s)
      : Exception(cause)
    {
        assert(s);
        std::strncpy(m_string, s, MaxStringLength);
    }
    explicit StringException(const char* s)
    {
        assert(s);
        std::strncpy(m_string, s, MaxStringLength);
    }

    // Return the string stored in the exception.
    const char* string() const
    {
        return m_string;
    }

  private:
    static const size_t MaxStringLength = 256;
    char m_string[MaxStringLength + 1];
};

}       // namespace foundation

#endif  // !CLASSLIB_EXCEPTION_H
